home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / grafix / raytracing / raylab / source / tga.c < prev    next >
C/C++ Source or Header  |  1996-02-17  |  2KB  |  78 lines

  1. /*
  2.     name:    tga.c
  3.  
  4.     Targa 24-bit color picture support
  5.     ----------------------------------
  6.  
  7.     These routines enables image output in 24-bit targa format.
  8.  
  9. */
  10.  
  11.  
  12. #include  <stdio.h>
  13. #include  <stdlib.h>
  14. #include  <string.h>
  15.  
  16. #include  "defs.h"
  17. #include  "tga.h"
  18. #include  "extern.h"
  19.  
  20.  
  21. void WriteTgaHeader(FILE *f,long width, long height)
  22. {
  23.     long    i;
  24.     char    tga_header[18];
  25.  
  26.     for(i=0;i<18;i++) { tga_header[i]=(char) 0; }    /* Init header (all entries = 0) */
  27.     tga_header[2]=(char) 2;
  28.     tga_header[12]=(char) (width&0xff);
  29.     tga_header[13]=(char) (width>>8);
  30.     tga_header[14]=(char) (height&0xff);
  31.     tga_header[15]=(char) (height>>8);
  32.     tga_header[16]=(char) 24;
  33.     tga_header[17]=(char) 0x20;    /* Make picture appear "right" */
  34.                     /* (upper left corner = first byte in body) */
  35.  
  36.     for(i=0;i<18;i++) { fputc(tga_header[i],f); }    /* Write header to file */
  37. }
  38.  
  39.  
  40. void ReadTgaHeader(FILE *f, long *width, long *height)
  41. {
  42.     long    i, temp;
  43.  
  44.     for(i=1;i<=12;i++) { temp=fgetc(f); }
  45.     temp=fgetc(f); temp+=(fgetc(f)<<8); memcpy(width,&temp,(size_t) sizeof(temp));
  46.     temp=fgetc(f); temp+=(fgetc(f)<<8); memcpy(height,&temp,(size_t) sizeof(temp));
  47.     (void) fgetc(f);    /* Skip information about image-depth... */
  48.     (void) fgetc(f);    /* ...and about "mirroring" the picture... */
  49.                 /* May add full support later. */
  50. }
  51.  
  52.  
  53.  
  54. void WriteTgaPixle(FILE *f, char r, char g, char b)
  55. {
  56.     fputc((char) b,f); fputc((char) g,f); fputc((char) r,f);
  57. }
  58.  
  59.  
  60. void ReadTgaPixle(FILE *f, int *r, int *g, int *b)
  61. {
  62.     *b=fgetc(f); *g=fgetc(f); *r=fgetc(f);
  63. }
  64.  
  65.  
  66. void WriteTgaLine(FILE *f, long width)
  67. {
  68.     long    i;
  69.     unsigned char    col;
  70.  
  71.     for(i=0;i<width;i++) {
  72.         col=pixlearray[(i*3)+2];          /* Switch r<->b (in tga files the */
  73.         pixlearray[(i*3)+2]=pixlearray[(i*3)+0];  /* color comes as b,g,r)          */
  74.         pixlearray[(i*3)+0]=col;    
  75.     }
  76.     (void) fwrite(pixlearray,sizeof(unsigned char),(size_t)width*3,f);
  77. }
  78.